package com.hkbigdata.datagovernance.governance.assess;

import com.hkbigdata.datagovernance.governance.bean.AssessParam;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessDetail;
import com.hkbigdata.datagovernance.governance.bean.GovernanceMetric;
import com.hkbigdata.datagovernance.meta.bean.TableMetaInfo;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.Date;

public abstract class Assessor {
    //TODO 注释修改
    public abstract void assess(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception;
    public GovernanceAssessDetail metricAssess(AssessParam assessParam) {
        //从assessParam中提取值
        GovernanceAssessDetail governanceAssessDetail =new GovernanceAssessDetail();
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        String assessDate = assessParam.getAssessDate();
        //根据相关信息对governanceAssessDetail进行初始化
        governanceAssessDetail.setMetricId(String.valueOf(governanceMetric.getId()));
        governanceAssessDetail.setMetricName(governanceMetric.getMetricName());
        governanceAssessDetail.setSchemaName(tableMetaInfo.getSchemaName());
        governanceAssessDetail.setTableName(tableMetaInfo.getTableName());
        governanceAssessDetail.setTecOwner(tableMetaInfo.getTableMetaInfoExtra().getTecOwnerUserName());
        //默认给满分
        governanceAssessDetail.setAssessScore(BigDecimal.valueOf(10));
        governanceAssessDetail.setAssessDate(assessDate);
        governanceAssessDetail.setGovernanceType(governanceMetric.getGovernanceType());
        try {
            //进行考评
            assess(governanceAssessDetail,assessParam);
        }catch (Exception e){
            //捕获异常写在考评错误信息中
            governanceAssessDetail.setIsAssessException("1");
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter=new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);

//            todo 这个方法可能不支持异步，待解决（可不解决）
//            governanceAssessDetail.setAssessExceptionMsg(stringWriter.toString().substring(0,2000));

            e.printStackTrace();
        }
        //设置创建时间
        governanceAssessDetail.setCreateTime(new Date());
        return  governanceAssessDetail;
    }
}
